package cn.gotom.core.web.action;

import java.util.HashSet;
import java.util.List;

import org.apache.log4j.Logger;

import cn.gotom.coding.EncodingUtils;
import cn.gotom.core.pojos.Right;
import cn.gotom.core.pojos.Role;
import cn.gotom.core.service.RightService;
import cn.gotom.core.service.RoleService;
import cn.gotom.core.web.Constants;

import com.google.inject.Inject;

public class RoleRightAction implements MenuInf
{
	protected final Logger log = Logger.getLogger(getClass());

	public String remove()
	{
		log.debug("remove id=" + role.getId());
		role = roleService.get(role.getId());
		if (role != null)
		{
			roleService.remove(role);
		}
		return Constants.LIST;
	}

	public String save()
	{
		role.setRights(new HashSet<Right>());
		rightList = rightService.findAll();
		if (ids != null)
		{
			for (String rightId : ids)
			{
				for (Right right : rightList)
				{
					if (right.getId().equals(rightId))
					{
						role.getRights().add(right);
						break;
					}
				}
			}
		}
		Role roleOld = roleService.get(role.getId());
		if (roleOld != null)
		{
			role.setUsers(roleOld.getUsers());
		}
		EncodingUtils.encoding(role, "GBK", "cp850");
		role = roleService.save(role);
		return Constants.LIST;
	}

	public String list()
	{
		roleList = roleService.findAll();
		for (Role r : roleList)
		{
			EncodingUtils.encoding(r, "cp850", "GBK");
		}
		rightList = rightService.findAll();
		for (Right r : rightList)
		{
			EncodingUtils.encoding(r, "cp850", "GBK");
		}
		if (role != null)
		{
			role = roleService.get(role.getId());
			if (role != null && role.getRights() != null)
			{
				for (Right right : rightList)
				{
					for (Right roleRight : role.getRights())
					{
						if (roleRight.getId().equals(right.getId()))
						{
							right.setChecked(true);
							break;
						}
					}
				}
			}
		}
		else
		{
			role = new Role();
		}
		return Constants.SUCCESS;
	}

	public String input()
	{
		role = null;
		return list();
	}

	@Inject
	private RightService rightService;

	@Inject
	private RoleService roleService;

	private Role role;

	private String[] ids;

	private List<Role> roleList;

	private List<Right> rightList;

	@Override
	public String getName()
	{
		return "角色管理";
	}

	@Override
	public String getGroupName()
	{
		return "系统管理";
	}

	@Override
	public int getMenuSort()
	{
		return 10001;
	}

	public Role getRole()
	{
		return role;
	}

	public void setRole(Role role)
	{
		this.role = role;
	}

	public List<Role> getRoleList()
	{
		return roleList;
	}

	public void setRoleList(List<Role> roleList)
	{
		this.roleList = roleList;
	}

	public List<Right> getRightList()
	{
		return rightList;
	}

	public void setRightList(List<Right> rightList)
	{
		this.rightList = rightList;
	}

	public String[] getIds()
	{
		return ids;
	}

	public void setIds(String[] ids)
	{
		this.ids = ids;
	}

}
